import numpy as np
import csv
import random
from datetime import datetime
from time import strftime


'''
Global and Local Empirical Bayes Smoothers with Gamma Model
'''

def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_data_list(inputCSV):
    sKey = []
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = float(record[item])
            sKey.append(temp)
    sKey = np.array(sKey)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey



#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    print '===================================================='
    print "begin at " + getCurTime()

    nocentroid = [2013, 2016, 2020, 2050, 2060, 2068, 2070, 2090, 2100, 2105, 2110, 2122, 2130, 2150,
     2164, 2170, 2180, 2185, 2188, 2201, 2220, 2230, 2232, 2240, 2261, 2270, 2280, 2282,
     2290, 12025, 15001, 15003, 15007, 15009, 51560]
    filepath = 'C:/_DATA/migration89_08/COUNTY Migration/clean/distinct_fips.csv'
    countyInfo = build_data_list(filepath)
    print len(countyInfo), len(nocentroid)

    output = []
    for item in countyInfo:
        if int(item[0]) not in nocentroid:
            output.append(item)
    print len(output)
    
    #np.savetxt(filepath[:-4] + '_Continental.csv', output, delimiter=',', fmt = '%i')
    